package com.prp.gwt.client.widgets.user.php;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.friend.FriendHistoryHandler;
import com.prp.gwt.client.user.php.PHPHistoryHandler;
import com.prp.gwt.client.vo.User;
import com.prp.gwt.client.vo.UserList;

public class FriendsPreviewPanel extends DecoratorPanel{

	private static FriendsPreviewPanel instance;
	private VerticalPanel innerPanel = new VerticalPanel();
	private Grid table = new Grid(6, 3);
	private Image loadImage = new Image(GWT.getModuleBaseURL() + "loading.gif");
	private Label title = new Label();
	private UserList userlist;
	
	public static FriendsPreviewPanel getInstance() {
		return instance;
	}

	public FriendsPreviewPanel() {
		instance = this;
		addStyleName("friendsPreview");
		{
			HorizontalPanel hPanel = new HorizontalPanel();
			hPanel.addStyleName("friendsPreview-title");
			title.setText("我的邻友");
			title.addStyleName("friendsPreview-title-text");
			HTML link = new HTML("<a>查看全部</a>");
            link.addStyleName("friendsPreview-title-text");
			hPanel.add(title);
			hPanel.add(link);
			hPanel.setCellHorizontalAlignment(link, HorizontalPanel.ALIGN_RIGHT);
			innerPanel.add(hPanel);
			link.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					if(userlist.getSender().equals(userlist.getOwner()))
						History.newItem(FriendHistoryHandler.INIT_VALUE);
					else
						History.newItem(FriendHistoryHandler.PREFIX_FRIENDLIST+userlist.getOwner().getId());
				}
			});
		}
		innerPanel.add(loadImage);
		innerPanel.add(table);
		add(innerPanel);
	}

	public void init(){
		table.clear();
		loadImage.setVisible(true);
		title.setText("邻友");
		
	}
	
	public void load(UserList list) {
		userlist = list;
		loadImage.setVisible(false);
		if(list.getSender().equals(list.getOwner()))
			title.setText("我的邻友");
		else
			title.setText(list.getOwner().getUsername()+"的邻友");
		for (int i = 0; i < list.size() && i < 18; i++) {
			VerticalPanel panel = new VerticalPanel();
			Image image = new Image(list.get(i).getPortrait());
			image.addStyleName("friendsPreview-image");
			panel.add(image);
			final User user = list.get(i);
			HTML name = new HTML("<a>" + user.getUsername() + "</a>");
			name.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					History.newItem(PHPHistoryHandler.PREFIX_USERHOME+user.getId());
				}
			});
			name.addStyleName("friendsPreview-name");
			panel.add(name);
			panel.setCellHorizontalAlignment(name,
					HasHorizontalAlignment.ALIGN_CENTER);
			table.setWidget(i / 3, i % 3, panel);
			if (i / 3 != 5)
				table.getCellFormatter().addStyleName(i / 3, i % 3,
						"friendsPreview-item-top");
			if (i % 3 != 2)
				table.getCellFormatter().addStyleName(i / 3, i % 3,
						"friendsPreview-item-left");
		}
	}
	
}
